package com.weiyirong99.crm.app.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;



/**
 * @Description:
 * @Author: zdx
 * @CreateDate: 2020/9/14
 * @UpdateDate: 2020/9/14
 * @Version: 1.0
 */

public abstract class BaseFragment extends Fragment {
    public final String TAG = this.getClass().getName();
    public QMUITipDialog tipDialog;
    public Gson mGson=new Gson();
    public boolean isCanShowing = true;
    public BaseActivity mActivity;
    public View rootView = null;
    public  static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public abstract int bindLayout();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(bindLayout(), container, false);
        initView(rootView);
        initData(savedInstanceState);
        return rootView;
    }


    public abstract View initView(final View view);


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    public  void initData(Bundle bundle){

    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            mActivity = (BaseActivity) context;
        } else {
            Toast.makeText(context, "初始化错误", Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * findViewById 简化 不需类型强转
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T $(View view, int resId) {
        return view.findViewById(resId);
    }

    @Override
    public void onResume() {
        super.onResume();
        isCanShowing = isVisible();

    }

    @Override
    public void onStop() {
        super.onStop();
        isCanShowing = false;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isCanShowing = !hidden;
        onVisibleChanged(hidden);
    }

    protected void onVisibleChanged(boolean isVisible) {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


    @Subscribe
    public void onEventMainThread(String flags) {

    }

}
